﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

using Centrica.Task.Model;
using Centrica.Task.App.Model;

namespace Centrica.Task.App.ViewModel
{
    public class TaskListViewModel
    {
        protected TaskListViewModel()
        {
        }

        public static TaskListViewModel CreateInstance()
        {
            return new TaskListViewModel();
        }

        public static XmlDocument Xml
        {
            get
            {
                List<Centrica.Task.Model.Task> taskList =
                    TaskModelFactoryProvider.StaticInstance.Provider.GetAllTasks();

                XDocument xDocument = new XDocument();

                XElement xInfo = new XElement(@"Info");
                xDocument.Add(xInfo);

                taskList.ForEach(t =>
                {
                    xInfo.Add(
                            new XElement(@"Item",
                                new XAttribute(@"Category", t.Description),
                                new XAttribute(@"Name", t.Name),
                                new XAttribute(@"Owner", t.Owner),
                                new XAttribute(@"Status", t.Status),
                                new XAttribute(@"__ID", t.ID)
                            ),
                            new XElement(@"Item",
                                new XAttribute(@"Category", t.Description),
                                new XAttribute(@"Owner", t.StartDate.ToString(@"dd MMM yyyy")),
                                new XAttribute(@"Status", t.FinishDate.ToString(@"dd MMM yyyy")),
                                new XAttribute(@"__ID", t.ID)
                            )
                    );
                });

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xDocument.ToString());

                return xmlDocument;
            }
        }
    }
}
